package org.alex.samples.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IExamUserDAO;
import org.alex.samples.javabean.ExamUserVO;
import org.alex.samples.model.ExamUser;
import org.alex.samples.util.ExcelUtils;
import org.alex.samples.util.MD5Utils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ExamUserServiceImpl implements IExamUserService {
	
	@Autowired
	private IExamUserDAO iExamUserDAO;
	
	@Autowired
	private IUserAndSubjectService iUserAndSubjectService;
	
	@Override
	public List<ExamUserVO> findAll() {
		List<ExamUserVO> examUserVOs = new ArrayList<>();
		List<ExamUser> examUsers = iExamUserDAO.findAll();
		ExamUserVO examUserVO = null;
		for(ExamUser examUser: examUsers) {
			examUserVO = new ExamUserVO();
			BeanUtils.copyProperties(examUser, examUserVO);
			examUserVOs.add(examUserVO);
		}
		return examUserVOs;
	}

	@Override
	public void savaExamUserByExcel(InputStream inputStream, String postfix) {
		Workbook workbook = ExcelUtils.openWorkBook(inputStream, postfix);
		List<ExamUserVO> examUserVOs = ExcelUtils.getExamUserByExcel(workbook);
		List<ExamUser> examUsers = new ArrayList<ExamUser>();
		if(examUserVOs != null) {
			ExamUser examUser = null;
			for(int i = 0;i < examUserVOs.size();i++) {
				examUser = new ExamUser();
				BeanUtils.copyProperties(examUserVOs.get(i), examUser);
				
				examUsers.add(examUser);
			}
		}
		iExamUserDAO.save(examUsers);
	}

	@Override
	public void addExamUser(ExamUserVO userVO) {
		ExamUser examUser = new ExamUser();
		BeanUtils.copyProperties(userVO, examUser);
		examUser.setPassword(MD5Utils.getMD5Code(examUser.getPassword()));
		iExamUserDAO.save(examUser);
	}

	@Override
	public ExamUserVO getExamUserById(String userId) {
		ExamUser examUser = iExamUserDAO.get(userId);
		ExamUserVO examUserVO = null;
		if (examUser != null) {
			examUserVO = new ExamUserVO();
			BeanUtils.copyProperties(examUser, examUserVO);
		}
		return examUserVO;
	}

	@Override
	public void updateExamUserByExamUserVO(ExamUserVO userVO) {
//		ExamUser examUser = new ExamUser();
//		BeanUtils.copyProperties(userVO, examUser);
//		iExamUserDAO.update(examUser);
		ExamUser examUser = iExamUserDAO.get(userVO.getId());
		BeanUtils.copyProperties(userVO, examUser);
		iExamUserDAO.update(examUser);
	}
	
	

	@Override
	public void deleteExamUser(String userId) {
		ExamUser examUser = iExamUserDAO.get(userId);
		if (examUser != null) {
			iExamUserDAO.delete(examUser);
		}
	}

	@Override
	public void batchDeleteExamUser(String[] userId) {
		ExamUser examUser = null;
		for(int i = 0; i < userId.length;i++) {
			examUser = iExamUserDAO.get(userId[i].toString());
			if (examUser != null) {
				iExamUserDAO.delete(examUser);
			}
		}
	}

	@Override
	public void deleteAllExamUser() {
		List<ExamUser> examUsers = iExamUserDAO.findAll();
		iExamUserDAO.deleteAll(examUsers);
	}

	@Override
	public void updateExamUser(ExamUser examUser) {
		iExamUserDAO.update(examUser);
	}

	@Override
	public void saveUserAndSubject(List<String> list, Integer subjectId) {
		List<String> examUsers = iUserAndSubjectService.getAllUserBySubject(subjectId);
		
		List<String> insertList = new ArrayList<String>();
		List<String> deleteList = new ArrayList<String>();
		
		for(String selectedUser: list) {
			if (!examUsers.contains(selectedUser)) {
				insertList.add(selectedUser);
			}
		}
		
		for(String selectedUser: examUsers) {
			if(!list.contains(selectedUser)) {
				deleteList.add(selectedUser);
			}
		}
		
		iUserAndSubjectService.insertSubjectUsers(insertList, subjectId);
		iUserAndSubjectService.deleteSubjectUser(deleteList, subjectId);
	}

}
